package logic;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Serializable;

import logic.strategy.*;

public class MissionConquerContinent implements Mission, Serializable {
	
	private static final long serialVersionUID = 1L;
	private String mission;
	protected Continent continentToConquer;
	protected int numberOfSoldiers = 1;

	public MissionConquerContinent(Continent continent) {
		this.continentToConquer = continent;
		this.mission = "need to conquer " + continent + "\nThe Countries to counrqe are:\n";
		for(int i=0; i<continentToConquer.getTerritoriesContained().size()-1; i++){
			this.mission += continentToConquer.getTerritoriesContained().get(i).getName() + ", ";
		}
		this.mission += continentToConquer.getTerritoriesContained().get(continentToConquer.getTerritoriesContained().size()-1).getName();
	}
	
	/**
	 * create new conquer mission
	 * @param continent - the continent to conquer
	 * @param numberOfSoldiers - number of soldier the player has to place on each
	 * 							 contitnent's country.
	 */
	public MissionConquerContinent(Continent continent, int numberOfSoldiers){
		this.continentToConquer = continent;
		this.numberOfSoldiers = numberOfSoldiers;
		this.mission = "need to conquer " + continent + " by placing at least " + numberOfSoldiers 
						+ " soldiers on each country.\nThe Countries to counrqe are:\n";
		for(int i=0; i<continentToConquer.getTerritoriesContained().size()-1; i++){
			this.mission += continentToConquer.getTerritoriesContained().get(i).getName() + ", ";
		}
		this.mission += continentToConquer.getTerritoriesContained().get(continentToConquer.getTerritoriesContained().size()-1).getName();
	}
	
	public String getMission() {
		return this.mission;
	}
	
	public String toString() {
		return this.getMission();
	}
	
	public boolean isMissionAccomplished(Player player) {
		if(this.numberOfSoldiers == 1)
			return this.continentToConquer.isOwnedBy(player);
		else {
			return this.continentToConquer.isOwnedBy(player, numberOfSoldiers);
		}
	}
	
	public boolean isLegal(Player player){
		return true;
	}

	public boolean saveMission(BufferedWriter bwriter) {
		try {
			bwriter.write("conquer:" + this.continentToConquer.getName() + ":" + this.numberOfSoldiers);
		} catch (IOException e) {
			System.err.println("An error occured while saving, please try again later.");
			return false;
		}
		return true;
	}
	
	/**
	 * Return strategy applicable to Conquer Mission
	 * MUST initialize strategy using the init method, otherwise 
	 * won't work.
	 */
	public Strategy getStrategy() {
		return new ConquerStrategy(continentToConquer);
		//return new RandomStrategy();
	}

	/**
	 * return true iff the missions are equal
	 */
	public boolean equals(Mission mission) {
		if(!(mission instanceof MissionConquerContinent)) return false;
		MissionConquerContinent temp = (MissionConquerContinent)mission;
		if(!temp.continentToConquer.equals(this.continentToConquer)) return false;
		return true;
	}

}
